package com.learn.thread;

import org.springframework.util.Assert;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author Created by chenxin34 on 2018/8/22 下午8:02.
 */
public class ThreadPoolExitTest {
    public static void main(String[] args) throws InterruptedException {
        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(10, 1000, 1000, TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<Runnable>(10));



        ThreadRunnable runnable = new ThreadRunnable();
        poolExecutor.submit(runnable);

        Thread.sleep(3000);

        System.out.println("main thread here");

        System.out.println("thread pool is shutdown = " + poolExecutor.isShutdown());

        System.out.println("pool active count = " + poolExecutor.getActiveCount());

        Assert.isTrue(runnable.getThread().getName().equals("inner-runnable-pool-son-thread"));

        System.out.println("alive = " + runnable.getThread().isAlive());
        System.out.println("daemon = " + runnable.getThread().isDaemon());
        System.out.println("state = " + runnable.getThread().getState());

//        poolExecutor.shutdown();


    }

    private static class ThreadRunnable implements Runnable{
        private Thread thread;

        public Thread getThread() {
            return thread;
        }

        public void setThread(Thread thread) {
            this.thread = thread;
        }


        public void run2() {
            thread = new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + " run");
            }, "inner-runnable-pool-son-thread");
            thread.start();

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("alive = " + thread.isAlive());
        }

        @Override
        public void run() {

        }
    }
}
